package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.dto.SetmealDto;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author spt
 * @description 套餐SEVICE实现类
 * @date 2019/9/26
 **/
@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}", setmeal, checkgroupIds);
        // 调用DAO数据入库
        // 插入基本数据
        setMealDao.insert(setmeal);
        // 插入关联数据
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.insertSetMealAndCheckGroup(setmeal.getId(), checkgroupId);
        }

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //DAO查询数据
        Page<Setmeal> page = setMealDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public long getNumOfImg(String rubbishImg) {
        return setMealDao.getNumOfImg(rubbishImg);
    }


    /**
     * 查询套餐id
     *
     * @param id
     * @return
     */
    @Override
    public Setmeal selectId(Integer id) {
        return setMealDao.findById(id);
    }

    @Override
    public List<Setmeal> findAll() {
        return setMealDao.selectAll(); // 查询手机套餐全部信息
    }

    //拿到所有套餐的name
    @Override
    public Set<String> getnames() {

        Set<String> aa = setMealDao.selectname();
        return aa;
    }

    @Override
    public ArrayList<SetmealDto> getnamesandshu() {
        ArrayList<SetmealDto> bb = setMealDao.selectnameand();
        return bb;
    }

    //永远获取最新的十二个月
    @Override
    public HashMap<String, Object> getyuefen() {
        HashMap<String, Object> hashMap1 = new HashMap<>();
        List<String> dateList = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        //取当前时间12个月内的年月
        for (int i = 0; i < 12; i++) {
            int k = c.get(Calendar.YEAR);
            int j = c.get(Calendar.MONTH) + 1 - i;
            String date = "";
            if (j >= 1) {
                date = k +"." + (j >= 10 ? "" : "0") + j;
            } else {
                int p = 11 - i;
                int m = c.get(Calendar.YEAR) - 1;
                int n = c.get(Calendar.MONTH) + 2 + p;
                date = m +"." + (n >= 10 ? "" : "0") + n;
            }
            dateList.add(date);
        }
        Collections.reverse(dateList);
        ArrayList<Integer> hashMap = new ArrayList<>();
        for (String s : dateList) {
            Integer a = setMealDao.selectdangyuehuiyuan(s);
            hashMap.add(a);
        }
        hashMap1.put("months",dateList);
        hashMap1.put("memberCount",hashMap);
        return hashMap1;
    }
}
